<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./css/main.css" />
  </head>
  <body>
    <div class="dialog" id="dialog">
      <div class="content">
        <button class="icon-close" data-action="close">x</button>
      </div>
    </div>

    <h1>hello APP</h1>

    <button data-action="open">open</button>

    <script>
      const dialog = document.getElementById("dialog");
      const actions = {
        close() {
          dialog.style.display = "none";
        },
        open() {
          dialog.style.display = "flex";
        },
      };
      document.body.onclick = function (e) {
        // 获取事件行为标识
        const action = e.target.dataset.action;
        // 过滤条件，不符合条件代码就不执行
        if (action === undefined || actions[action] === undefined) return;
        // 修改方法中this
        actions[action].call(e.target);
        // actions[action]();
      };
    </script>
  </body>
</html>
